package com.sh.rbac.controller;

import com.sh.rbac.core.common.result.Result;
import com.sh.rbac.domain.dto.user.UserQueryDTO;
import com.sh.rbac.domain.entity.Role;
import com.sh.rbac.domain.entity.User;
import com.sh.rbac.service.OnlineUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 在线用户控制器
 *
 * @author wb
 * @since 2025-10-13
 */

@Tag(name = "在线用户管理", description = "在线用户相关操作")
@RestController
@RequestMapping("sys/online-user")
public class OnlineUserController {

    @Autowired
    private OnlineUserService onlineUserService;

    @Operation(summary = "分页查询在线用户", description = "条件查询分页查询在线用户")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = User.class)))
    @PostMapping("page/{current}/{limit}")
    @PreAuthorize("@ps.hasPerm('sys:online-user:page')")
    public Result<?> getPage(@PathVariable Long current, @PathVariable Long limit, @RequestBody UserQueryDTO userQueryDTO) {
        return Result.success(onlineUserService.getPage(current, limit, userQueryDTO));
    }


    @Operation(summary = "强退在线用户", description = "根据token强退在线用户")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = Role.class)))
    @GetMapping("quit")
    @PreAuthorize("@ps.hasPerm('sys:online-user:quit')")
    public Result<?> get(@RequestParam("token") String token) {
        return onlineUserService.quitUser(token) ? Result.success() : Result.failure();
    }
}
